package com.bed.lovewithpatientbed.service.impl;

import com.bed.lovewithpatientbed.dao.WorkerMessageDao;
import com.bed.lovewithpatientbed.pojo.Role;
import com.bed.lovewithpatientbed.pojo.WorkerMessage;
import com.bed.lovewithpatientbed.pojo.WorkerMessageExample;
import com.bed.lovewithpatientbed.service.IRoleService;
import com.bed.lovewithpatientbed.service.IWorkMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author xuxiaoding
 * @version 1.0.0
 * @ClassName WorkMessageServiceImpl.java
 * @Description TODO
 */
@Service
public class WorkMessageServiceImpl implements IWorkMessageService {

    private WorkerMessageDao workerMessageDao;

    private IRoleService roleService;

    @Autowired
    public void setWorkerMessageDao(WorkerMessageDao workerMessageDao) {
        this.workerMessageDao = workerMessageDao;
    }

    @Autowired
    public void setRoleService(IRoleService roleService) {
        this.roleService = roleService;
    }

    @Override
    public WorkerMessage retrieveWorkMessageByPhone(String workMessagePhone) {
        if (workMessagePhone == null || "".equals(workMessagePhone)) {
            return null;
        }

        WorkerMessageExample workerMessageExample = new WorkerMessageExample();
        workerMessageExample.createCriteria().andWorkMessagePhoneEqualTo(workMessagePhone);
        List<WorkerMessage> workerMessageList = workerMessageDao.selectByExample(workerMessageExample);
        if (!CollectionUtils.isEmpty(workerMessageList)) {
            return workerMessageList.get(0);
        } else {
            return null;
        }
    }

    @Override
    public Role getRoleByRoleId(Integer roleId) {
        return roleService.retrieveRoleByRoleId(roleId);
    }
    /**
     * @description 重置密码
     * @author tyl
     * @updateTime 2020/10/22 19:18
     * @param workerMessage 人员
     */
    @Override
    public void resetPassword(WorkerMessage workerMessage) {
        WorkerMessageExample example = new WorkerMessageExample();
        WorkerMessageExample.Criteria criteria = example.createCriteria();
        criteria.andWorkerMessageIdEqualTo(workerMessage.getWorkerMessageId());
        workerMessage.setWorkMessagePassword("000000");
        workerMessageDao.updateByPrimaryKeySelective(workerMessage);
    }

    @Override
    public WorkerMessage selectByPhoneAndPwd(WorkerMessage WorkerMessage) {
        WorkerMessageExample example = new WorkerMessageExample();
        WorkerMessageExample.Criteria criteria = example.createCriteria();
        if(WorkerMessage!=null) {
            criteria.andWorkMessagePhoneEqualTo(WorkerMessage.getWorkMessagePhone());
            criteria.andWorkMessagePasswordEqualTo(WorkerMessage.getWorkMessagePassword());
        }
        List<WorkerMessage> workerMessageList = workerMessageDao.selectByExample(example);
        return workerMessageList.size()==0?null:workerMessageList.get(0);
    }
}
